/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.security;

/**
 * An abstract permissions object where a group of permission values are stored.
 * @author Adam
 * @version $Id: AbstractPermissions.java 25 2009-05-26 04:23:01Z a.ruggles $
 * 
 * Created on Mar 5, 2009 at 8:29:01 PM 
 */
public abstract class AbstractPermissions {
	/**
	 * Permission value. The value represents 32 bits for each permission.
	 */
	private int value;

	/**
	 * Default constructor.
	 */
	protected AbstractPermissions() {
		this.value = 0; // No Permissions.
	}

	/**
	 * Constructs an abstract permission with an initial value.
	 * @param value The value that represents the permission.
	 */
	protected AbstractPermissions(final int value) {
		this.value = value;
	}

	/**
	 * Adds the value of a group of permission using logical or.
	 * @param permission The permission of the group of permissions to add.
	 */
	protected void add(final int permission) {
		this.value |= permission;
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.security.Permissions#add(com.google.code.sapien.security.Permissions)
	 */
	public void add(final AbstractPermissions permissions) {
		this.value |=  permissions.value;
	}

	/**
	 * Sets this entry to that in the specified permissions.
	 * @param permissions The permissions to be set.
	 */
	public void set(final AbstractPermissions permissions) {
		this.value = permissions.value;
	}

	/**
	 * Returns an integer value representing a group of permissions. 
	 * @return An integer value representing a group of permissions. 
	 */
	protected int getValue() {
		return value;
	}

	/**
	 * Determines if the value contains the passed in permission value.
	 * @param permission The permission value.
	 * @return True if the value has the permission, otherwise false.
	 */
	public boolean has(final int permission) {
		return isValueSet(permission);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.security.Permissions#check(com.google.code.sapien.security.Permissions)
	 */
	public boolean check(final AbstractPermissions permissions) {
		return isValueSet(permissions.value);
	}

	/**
	 * Checks to see if the permission values are set to true.
	 * @param checkValue The permission values to check.
	 * @return True if all the check values are set in this group of permissions.
	 */
	protected boolean isValueSet(final int checkValue) {
		return ((this.value & checkValue) == checkValue);
	}

	/**
	 * Removes the value of a group of permissions using logical and.
	 * @param permission The permission of the group of permissions to remove.
	 */
	protected void remove(final int permission) {
		this.value &= ~permission;
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.security.Permissions#remove(com.google.code.sapien.security.Permissions)
	 */
	public void remove(final AbstractPermissions permissions) {
		this.value &= ~permissions.value;
	}

	/**
	 * Sets the integer value representing a group of permissions.
	 * @param value The value to set.
	 */
	protected void setValue(final int value) {
		this.value = value;
	}
}
